/*
 * Copyright 2010 Katsunori Koyanagi
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package sweezy.anim.impl
{
	import sweezy.anim.core.IAnimation;
	import sweezy.anim.core.IAnimationGroup;

	internal class AnimationGroupBase extends AnimationBase implements IAnimationGroup
	{

		protected var _animationInfo:AnimationGroupInfo;

		protected var _numAnimations:uint;

		public function AnimationGroupBase(manager:AnimationManager, animations:Array)
		{
			super(manager);

			for (var i:int = animations.length - 1; i >= 0; --i)
			{
				_animationInfo = new AnimationGroupInfo(animations[i], _animationInfo);
			}

			_numAnimations = animations.length;
		}

		public function get animations():Array
		{
			var animations:Array = [];
			var info:AnimationGroupInfo = _animationInfo;
			var index:int = 0;

			while (info !== null)
			{
				animations[index++] = info.animation;
				info = info.next;
			}

			return animations;
		}

		public function getAnimationAt(index:uint):IAnimation
		{
			if (_numAnimations >= index || index < 0)
			{
				return null;
			}

			var info:AnimationGroupInfo = _animationInfo;
			while (--index > -1)
			{
				info = info.next;
			}

			return info.animation;
		}

		public function getAnimationIndex(animation:IAnimation):int
		{
			if (animation === null)
			{
				return -1;
			}

			var info:AnimationGroupInfo = _animationInfo;
			var index:int = 0;

			while (info !== null)
			{
				if (info.animation === animation)
				{
					return index;
				}

				index++;
				info = info.next;
			}

			return -1;
		}

		public function get numAnimations():uint
		{
			return _numAnimations;
		}

		public function toString():String
		{
			return animationName + "(duration=" + _duration + ", position=" + _position + ", children=[" + animations.join(", ") + "])";
		}
	}
}
